package com.nccsjz.back.game.service;
import java.io.File;
import java.sql.SQLException;
import java.util.List;

import com.nccsjz.back.game.dao.GameDAO;
import com.nccsjz.base.BaseService;
import com.nccsjz.pojo.Game;
import com.nccsjz.utils.Pager;
public class GameService extends BaseService{
   //add game
	public boolean addGame(Game game) throws SQLException{
	    boolean flag=false;
	    try{
	    	openCon();
	    	flag=new GameDAO(con).addGame(game);
	    }finally{
	    	closeCon();
	    }
	    return flag;
   }
   //delete game
	public boolean deleteGame(Long id) throws SQLException{
		boolean flag=false;
		try{
			openCon();
			flag=new GameDAO(con).deleteGame(id);
			
		}finally{
			closeCon();
		}
		return flag;
	}
	/**
	 * 删除游戏所对应的磁盘文件
	 * @param gid
	 * @return
	 * @throws SQLException
	 */
	public boolean deleteGameFile(Long gid) throws SQLException {
		Game g = this.getGame(gid);
		boolean flag=false;
		if (null != g) {
			//游戏图片
			if (null != g.getGamepic()) {
				File fPic = new File(g.getGamepic());
				if(fPic.exists()){
					fPic.delete();
				}
				
			}
			//游戏
			if (null != g.getGamepic()) {
				File fG = new File(g.getGamepath());
				if(fG.exists()){
					fG.delete();
				}
			}
		}
		return flag;
	}

	//batch delete game
	public boolean deldetBatchGame(List<Long> list) throws SQLException{
		boolean flag=false;
		try{
			openCon();
			con.setAutoCommit(false);
			flag=new GameDAO(con).deleteBatchGame(list);
			con.commit();
		}finally{
			closeCon();
		}
		return flag;
	}
	//pager display game
	public Pager getGame(int pageSize,int pageNo) throws SQLException{
		  Pager pager=null;
		  try{
			  openCon();
			  pager=new GameDAO(con).getGame(pageSize, pageNo);
		  }finally{
			  closeCon();
		  }
		  return pager;
	}
	//pager display game by name
	public Pager getGame(int pageSize,int pageNo,String title) throws SQLException{
		Pager pager=null;
		try{
			openCon();
			pager=new GameDAO(con).getGame(pageSize, pageNo, title);
		}finally{
			closeCon();
		}
		return pager;
	}
	//get one game
	public  Game getGame(Long id) throws SQLException{
		Game game=null;
		try{
		  openCon();
		  game=new GameDAO(con).getGame(id);
		}finally{
		  closeCon();
		}
		return game;
	}
}
